package cn.com.nes.site.controller.app;

import cn.com.nes.common.util.DBUtils;
import cn.com.nes.common.util.SMSUtil;
import cn.com.nes.common.util.ValidCodeUtil;
import cn.com.nes.mybatis.integrateServ.entity.AppUser;
import cn.com.nes.site.entity.DataReq;
import cn.com.nes.site.entity.DataRes;
import cn.com.nes.site.entity.PageData;
import cn.com.nes.site.entity.sms.VerCodeReq;
import cn.com.nes.site.module.service.UserService;
import cn.com.nes.site.service.app.AppService;
import cn.com.nes.site.service.app.AppUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

    @Slf4j
    @Controller
    @RequestMapping("v2/app")
    public class AppLoginController {
        @Resource
        private AppUserService appUserService;
        //获取验证码
        @RequestMapping(method = RequestMethod.POST, value = "/sendValidCode")
        public @ResponseBody PageData getValidCode(HttpServletRequest request,
                                                   HttpServletResponse response, ModelMap modelMap) throws Exception {
            DataRes req_Status = DBUtils.setRemoteVisit(request, response,false);
            PageData res = new PageData() ;
            if (req_Status.getRetCode() != 0) {
                res = new PageData();
                res.setRetCode(req_Status.getRetCode()) ;
                res.setRetMessage(req_Status.getRetMessage());
                return res;
            }
            DataReq req = req_Status.getReq();
            String phone = (String) req.getReqData().get("phone");
            String smsType ="9";
            if ("".equals(phone) || phone==null) {
                res.setRetCode(9);
                res.setRetMessage("请输入手机号！");
                return res;
            }
            //验证手机号的唯一性
            if (appUserService.checkUser(req)) {
                res.setRetCode(9);
                res.setRetMessage("该手机号已经绑定过用户！");
            } else {
                //获取验证码
                String validCode = ValidCodeUtil.generateCode(phone);
                VerCodeReq sms = SMSUtil.sendSMS_ALIYUN_REG(phone, validCode);
                if (sms != null)
                {   sms.setSmstype(smsType);
                    appUserService.insertAppSmsCode(sms);
                    res.setRetCode(0);
                    res.setRetMessage("获取验证码成功！");
                }
                else
                {	res.setRetCode(9);
                    res.setRetMessage("获取验证码失败");
                }

            }
            return res;
        }
        //登录
        @RequestMapping(method = RequestMethod.POST, value = "/login")
        public @ResponseBody  PageData login(AppUser appUser) throws Exception {
            return appUserService.login(appUser);
        }
        //注册
        @RequestMapping(method = RequestMethod.POST, value = "/userReg")
        public   @ResponseBody PageData userReg(AppUser appUser) {
            return appUserService.appUserReg(appUser);
        }
        //修改密码
        @RequestMapping(method = RequestMethod.POST, value = "/updatePassword")
        public @ResponseBody  PageData updatePassword(AppUser appUser) {
            return appUserService.updatePassword(appUser) ;
        }

        //查询个人信息
        @RequestMapping(method = RequestMethod.POST, value = "/getUserMesg")
        public @ResponseBody  PageData getUserMesg(AppUser appUser) {
            return appUserService.getUserMesg(appUser);
        }

    }
